package org.yelongframework.zip.ant.executor;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.yelongframework.lang.Nullable;
import org.yelongframework.zip.executor.AbstractZipExecutor;

/**
 * 抽象实现
 * 
 * @since 1.0.0
 */
public abstract class AbstractAntZipExecutor extends AbstractZipExecutor implements AntZipExecutor {

	/** 编码 */
	@Nullable
	private String encoding;

	// ==================================================编码==================================================

	public AbstractAntZipExecutor() {
	}

	/**
	 * @param encoding 编码
	 */
	public AbstractAntZipExecutor(@Nullable String encoding) {
		this.encoding = encoding;
	}

	// ==================================================编码==================================================

	@Override
	public String getEncoding() {
		return encoding;
	}

	@Override
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	// ==================================================构建压缩包输出流==================================================

	/**
	 * 构建压缩包输出流
	 * 
	 * @param outputStream 输出流
	 * @return 压缩包数据流
	 */
	protected ZipOutputStream buildZipOutputStream(OutputStream outputStream) {
		ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
		if (null != encoding) {
			zipOutputStream.setEncoding(encoding);
		}
		return zipOutputStream;
	}

	// ==================================================构建压缩包输出流==================================================

	/**
	 * 构建压缩文件实例
	 * 
	 * @param file 压缩文件
	 * @return 压缩文件实例
	 * @throws IOException 流异常
	 */
	protected ZipFile buildZipFile(File file) throws IOException {
		ZipFile zipFile;
		if (null == encoding) {
			zipFile = new ZipFile(file);
		} else {
			zipFile = new ZipFile(file, encoding);
		}
		return zipFile;
	}

}
